package SAXParser;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class NaverRank extends DefaultHandler{
	 
	boolean flagK = false;
	boolean flagS = false;
	boolean flagV = false;
	
	private RankPost post = new RankPost();
	private ArrayList<RankPost> posts = new ArrayList<RankPost>();

	public ArrayList<RankPost> getPost() {
		return posts;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if(qName.equals("K")) {
			post = new RankPost();
			flagK = true;
		}
		else if(qName.equals("S")) {
			flagS= true;
		}
		else if(qName.equals("V")) {
			flagV = true;
		}
	} // end startElement
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		if(flagK) {
			post.setK(new String(ch,start,length));
		}
		else if(flagS) {
			post.setS(new String(ch,start,length));
			System.out.println(post.getS());
		}
		else if(flagV) {
			post.setV(new String(ch,start,length));
		}
	}// end characters
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		
		if(qName.equals("K")) {
			flagK = false;
		}
		else if(qName.equals("S")) {
			System.out.println(post.getS());
			flagS = false;
		}
		else if(qName.equals("V")) {
			flagV = false;
			posts.add(post);
		}
		
	}//end endElement
}
